package task

import (
	"context"
	"google.golang.org/protobuf/types/known/timestamppb"
	"wx-dailydoer/database/mysql"
	"wx-dailydoer/pb"
)

var (
	defaultTaskList = []*pb.Task{
		{
			Id:         0,
			Name:       "task1",
			Desc:       "这是task1",
			Freq:       0,
			StartedAt:  timestamppb.Now(),
			EndedAt:    timestamppb.Now(),
			CreatedAt:  timestamppb.Now(),
			DaysInWeek: []int64{1, 2},
			Tags:       []string{"Important", "Personal"},
		},
	}
)

func List(ctx context.Context, request *pb.EmptyRequest) (*pb.ListTasksResponse, error) {

	tasks, err := mysql.TaskDao.GetAll(ctx)
	if err != nil {
		return nil, err
	}

	res := []*pb.Task{}
	for _, task := range tasks {
		res = append(res, task.ConvertToDTO())
	}
	if len(res) != 0 {
		return &pb.ListTasksResponse{
			Total: int64(len(res)),
			Tasks: res,
		}, nil
	}
	return &pb.ListTasksResponse{
		Total: 10,
		Tasks: defaultTaskList,
	}, nil
}
